package online.foxnull2.foxmall.ware.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import online.foxnull2.foxmall.ware.entity.PurchaseDetailEntity;
import online.foxnull2.foxmall.ware.mapper.PurchaseDetailMapper;
import online.foxnull2.foxmall.ware.service.PurchaseDetailService;
import online.foxnull2.foxmall.ware.utils.PageUtils;
import online.foxnull2.foxmall.ware.utils.Query;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;

import java.util.Map;


@Service("purchaseDetailService")
public class PurchaseDetailServiceImpl extends ServiceImpl<PurchaseDetailMapper, PurchaseDetailEntity> implements PurchaseDetailService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<PurchaseDetailEntity> queryWrapper = new QueryWrapper<>();
        Object key = params.get("key");
        if (ObjectUtils.isNotEmpty(key)) {
            queryWrapper.and(wrapper -> queryWrapper.eq("purchase_id", key).or().eq("sku_id", key));
        }
        Object wareId = params.get("wareId");
        if (ObjectUtils.isNotEmpty(wareId)) {
            queryWrapper.eq("ware_id", wareId);
        }
        Object status = params.get("status");
        if (ObjectUtils.isNotEmpty(status)) {
            queryWrapper.eq("status", status);
        }

        IPage<PurchaseDetailEntity> page = this.page(
                new Query<PurchaseDetailEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

}